package org.jeecg.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.domain.City;
import org.jeecg.domain.CityDirectory;
import org.jeecg.domain.Region;
import org.jeecg.domain.Serve;
import org.jeecg.domain.dto.ResponseResult;
import org.jeecg.service.CityDirectoryService;
import org.jeecg.service.CityService;
import org.jeecg.service.RegionService;
import org.jeecg.mapper.RegionMapper;
import org.jeecg.service.ServeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.checkerframework.checker.units.qual.Prefix.one;

/**
* @author 86182
* @description 针对表【region(区域表)】的数据库操作Service实现
* @createDate 2024-12-19 14:25:57
*/
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region>
    implements RegionService{
    @Autowired
   private CityService cityDirectoryService;
    @Autowired
    private ServeService serveService;
    //    区域管理列表  分页
    @Override
    public ResponseResult regionList(Integer pageNum, Integer pageSize) {
        IPage<Region> page = new Page<>(pageNum, pageSize);
        this.page(page);
        return ResponseResult.okResult(page);
    }
    //    新增区域
    @Override
    public ResponseResult addRegion(Region region) {
        City one2 = cityDirectoryService.getOne(Wrappers.<City>lambdaQuery().eq(City::getName, region.getName()));
        Region one1 = this.getOne(Wrappers.<Region>lambdaQuery().eq(Region::getName, region.getName()));
        if (one1 == null){
            region.setCityCode(one2.getCode());
            boolean save = this.save(region);
            return save?ResponseResult.okResult("新增成功"):ResponseResult.errorResult(500,"新增失败");
        }else{
            return ResponseResult.errorResult(500,"新增失败,该区域已存在");
        }

    }
    //    删除  区域启用后不允许删除
    @Override
    public ResponseResult delRegion(Region region) {

        if (region.getActiveStatus() != null && region.getActiveStatus() == 2){
            return ResponseResult.errorResult(400,"正在启用，不能删除");
        }else{
            boolean removeById = this.removeById(region.getRid());
            return removeById?ResponseResult.okResult("删除成功"):ResponseResult.errorResult(500,"删除失败");
        }

    }
    //    区域存在上架的服务方可启用区域
    @Override
    public ResponseResult qiYongRegion(Region region) {
        List<Serve> list = serveService.list(Wrappers.<Serve>lambdaQuery().eq(Serve::getRegionId, region.getRid()));
        if (list != null && !list.isEmpty()){
            region.setActiveStatus(2);
            this.updateById(region);
            return ResponseResult.okResult("启用成功");
        }
        return ResponseResult.errorResult(400,"启用失败,区域不存在上架的服务");
    }

    //    修改区域
    @Override
    public ResponseResult updateRegion(Region region) {
        boolean b = this.updateById(region);
        return b?ResponseResult.okResult("修改成功"):ResponseResult.errorResult(500,"修改失败");
    }
}




